package com.sun.api;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.Cursor;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ScanOptions;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

@RestController
public class RedisTemplateSet {

    @Autowired
    private RedisTemplate redisTemplate;

    @RequestMapping("/set")
    public String testSet(){
        // 添加元素
        redisTemplate.opsForSet().add("set", "第一个set元素");
        redisTemplate.opsForSet().add("set", "第二个set元素");
        redisTemplate.opsForSet().add("set", "第三个set元素");
        redisTemplate.opsForSet().add("set", "第四个set元素");
        redisTemplate.opsForSet().add("set", "第五个set元素");

        // 获取集合中所有元素
        System.out.println(redisTemplate.opsForSet().members("set"));

        // 移除元素
        redisTemplate.opsForSet().remove("set", "第一个set元素", "第三个set元素");
        System.out.println(redisTemplate.opsForSet().members("set"));

        // 删除并返回一个随机元素
        System.out.println(redisTemplate.opsForSet().pop("set"));


        // 获取集合大小
        System.out.println(redisTemplate.opsForSet().size("set"));

        // 判断集合是否包含value
        System.out.println(redisTemplate.opsForSet().isMember("set", "第四个set元素"));

        redisTemplate.opsForSet().add("otherSet", "第一个set元素");
        redisTemplate.opsForSet().add("otherSet", "第二个set元素");
        redisTemplate.opsForSet().add("otherSet", "第三个set元素");
        redisTemplate.opsForSet().add("otherSet", "第四个set元素");
        // 获取两个集合的交集
        System.out.println(redisTemplate.opsForSet().intersect("set", "otherSet"));

        // 获取多个集合的交集
        redisTemplate.opsForSet().add("thirdSet", "第二个set元素");
        Set otherKeys = new HashSet();
        otherKeys.add("otherSet");
        otherKeys.add("thirdSet");
        System.out.println(redisTemplate.opsForSet().intersect("set", otherKeys));

        // set集合与otherSet集合的交集存储到targetSet集合中
        redisTemplate.opsForSet().intersectAndStore("set", "otherSet", "targetSet");
        System.out.println(redisTemplate.opsForSet().members("targetSet"));

        // set集合与多个集合的交集存储到targetSet集合中
        redisTemplate.opsForSet().intersectAndStore("set", otherKeys, "targetSet");
        System.out.println(redisTemplate.opsForSet().members("targetSet"));

        // 获取两个或者多个集合的并集
        System.out.println(redisTemplate.opsForSet().union("set", "otherSet"));
        System.out.println(redisTemplate.opsForSet().union("set", otherKeys));

        // set集合与多个集合的并集存储到targetSet集合中
        redisTemplate.opsForSet().unionAndStore("set", otherKeys, "targetSet2");
        System.out.println(redisTemplate.opsForSet().members("targetSets"));

        // 获取两个或者多个集合的差集
        System.out.println(redisTemplate.opsForSet().difference("set", otherKeys));

        // 差集存储到targetSet中
        redisTemplate.opsForSet().unionAndStore("set", otherKeys, "targetSet3");
        System.out.println(redisTemplate.opsForSet().members("targetSet3"));

        // 随机获取集合中的一个元素
        System.out.println(redisTemplate.opsForSet().randomMember("set"));

        // 随机获取集合中若干个元素
        System.out.println(redisTemplate.opsForSet().randomMembers("set", 5));

        // 遍历set，类似于iterator
        try {
            Cursor<Map.Entry<Object, Object>> cursor = redisTemplate.opsForSet().scan("set", ScanOptions.NONE);
            while (cursor.hasNext()) {
                System.out.println(cursor.next());
            }
            cursor.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }
}
